var _n46;
var cc__extends = __extends;
var cc__decorate = __decorate;
var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $1$Loader = require("Loader");
var s = cc__importDefault($1$Loader);
var $zqcc_OverallSystem = require("zqcc_OverallSystem");
var $1$zqcc_pageBase = require("zqcc_pageBase");
var l = cc__importDefault($1$zqcc_pageBase);
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var h = [40, 40, 40, 40];
var def_Tujian = function (t) {
  function _ctor() {
    // var e = null !== t && t.apply(this, arguments) || this;
    // e.pageName = "图鉴";
    // e.list = null;
    // e.listItem = null;
    // e.progressNode = null;
    // e.num = null;
    // e.tabNode = null;
    // return e;

    // 执行 t 函数（如果存在），并获取实例；如果 t 为 null，则使用当前上下文
    const instance = (t !== null) ? t.apply(this, arguments) : this;

    // 初始化属性
    instance.pageName = "图鉴"; // 设置页面名称
    instance.list = null;        // 初始化列表
    instance.listItem = null;    // 初始化列表项
    instance.progressNode = null; // 初始化进度节点
    instance.num = null;         // 初始化数字
    instance.tabNode = null;     // 初始化标签节点

    return instance; // 返回实例
  }
  cc__extends(_ctor, t);
  _ctor.prototype.onLoad = function () { };
  _ctor.prototype.start = function () {
    var t = this;
    s.default.loadBundle("newTujian", function () {
      if (t.node && t.node.parent) {
        const currentLevel = $zqcc_OverallSystem.sers().userSrv.getFeibiaoData().level; // 获取当前等级
        let section = 1; // 初始化段落编号
        let offset = 0; // 初始化偏移量

        // 计算段落和偏移量
        if (currentLevel <= h[0] + h[1] && currentLevel > h[0]) {
          section = 2;
          offset = h[0];
        } else if (currentLevel <= h[0] + h[1] + h[2] && currentLevel > h[0] + h[1]) {
          section = 3;
          offset -= (h[0] + h[1]);
        } else if (currentLevel <= h[0] + h[1] + h[2] + h[3] && currentLevel > h[0] + h[1] + h[2]) {
          section = 4;
          offset = h[0] + h[1] + h[2];
        }

        // 更新加载状态
        t.loadingJson(section, offset);

        // 选中对应的标签
        t.tabNode.node.children[section - 1].getComponent(cc.Toggle).check();
      }
    }, true, true);
    this.node.opacity = 0;
    this.scheduleOnce(function () {
      // 设置节点的缩放及渐变动画的代码
      t.node.scale = 0.01;

      /**
       * 启动节点的并行动画
       * @function startTweenAnimation
       * @param {cc.Node} node - 要进行动画的节点
       */
      const startTweenAnimation = (node) => {
        // 使用并行动画
        cc.tween(node)
          .parallel(
            cc.tween(node).to(0.15, {
              opacity: 255
            }),
            cc.tween(node).to(0.2, {
              scale: 1
            }, cc.easeBackOut()),
            cc.tween(node).by(0.2, {
              y: 0
            })
          )
          .start();
      };

      // 调用动画函数
      startTweenAnimation(t.node);
    });
  };
  _ctor.prototype.loadingJson = function (t, e) {
    var o = this;

    // Set default values for parameters t and e
    t = (t === undefined) ? 1 : t;
    e = (e === undefined) ? 40 : e;

    // Cap t at a maximum value of 4
    if (t > 4) {
      t = 4;
    }

    // Load the resource bundle and initialize the list if successful
    s.default.loadBundleRes("newTujian", "json/图鉴" + t, function (n) {
      if (n && o.node && o.node.parent) {
        o.initList(n.json, t, e);
      }
    });
  };
  _ctor.prototype.initList = function (t, e, o) {
    var n = this;
    cc.log("------initList", Object.keys(t));

    var itemNames = [];
    var itemDetails = [];

    // Iterate over the object 't'
    for (var key in t) {
      if (Object.prototype.hasOwnProperty.call(t, key)) {
        var item = t[key];
        var displayName = item["描述"];

        // If e is less than 4, use item name instead
        if (e < 4) {
          displayName = item["物品名称"];
        }

        itemNames.push(displayName);

        itemDetails.push({
          物品名称: displayName,
          星级: item["星级"],
          资源路径: item["资源路径"],
          描述: item["描述"]
        });
      }
    }

    cc.log(JSON.stringify(itemNames));
    cc.log(itemDetails);

    // Fetch user data and level information
    $zqcc_OverallSystem.sers().userSrv.getTujianData();
    var currentLevel = $zqcc_OverallSystem.sers().userSrv.getFeibiaoData().level;

    this.list.removeAllPages();
    var scaleFactor = (e > 3) ? 1 : 0.8;

    var loadItemPage = function (index) {
      var page = f.list.getPages()[index];
      if (!page) {
        page = cc.instantiate(f.listItem);
        f.list.addPage(page);
      }
      page.active = true;

      var displayItem = function (offset) {
        var itemIndex = index + offset + o;
        if (itemIndex < currentLevel) {
          var itemPage = page.getChildByName("" + offset);
          itemPage.active = true;

          var _itemIndex = e + _a - 1
          itemPage.getChildByName("name").getComponent(cc.Label).string = itemDetails[_itemIndex]["物品名称"];
          itemPage.getChildByName("zhufu").getComponent(cc.Label).string = "";

          // Load item resources
          s.default.loadBundleRes("newTujian", "/" + itemDetails[_itemIndex]["资源路径"], function (resource) {
            if (resource && n.node && n.node.parent) {
              var candyNode = itemPage.getChildByName("candy");
              var width = resource.width;
              var height = resource.height;
              var scaleSize = Math.max(width, height);
              var scaledSize = (200 / scaleSize) * scaleFactor;

              candyNode.scale = scaledSize;
              candyNode.width = width;
              candyNode.height = height;
              candyNode.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(resource);
            }
          });
          f.list.setCurrentPageIndex(Math.floor(_itemIndex / 4));
        } else {
          if (itemIndex >= Object.values(t).length) {
            page.getChildByName("" + offset).active = false;
            return "break";
          }

          // Load resource while handling locked state
          s.default.loadBundleRes("newTujian", "/" + itemDetails[_itemIndex]["资源路径"], function (resource) {
            if (resource && n.node && n.node.parent) {
              var candyNode = itemPage.getChildByName("" + offset).getChildByName("candy");
              var width = resource.width;
              var height = resource.height;
              var scaleSize = Math.max(width, height);
              var scaledSize = (220 / scaleSize) * scaleFactor;

              candyNode.scale = scaledSize;
              candyNode.width = width;
              candyNode.height = height;
              candyNode.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(resource);
              candyNode.color = new cc.Color(68, 68, 68);
            }
          });

          // Set visibility for locked items
          itemPage.getChildByName("" + offset).active = true;
          itemPage.getChildByName("" + offset).getChildByName("name").active = false;
          itemPage.getChildByName("" + offset).getChildByName("zhufu").active = false;
          itemPage.getChildByName("" + offset).getChildByName("unLock").active = true;
          itemPage.getChildByName("" + offset).getChildByName("zhufu").getComponent(cc.Label).string = "";
        }
      };

      // Iterate through each item offset
      for (var offset = 1; offset <= 4; offset++) {
        if (displayItem(offset) === "break") {
          break;
        }
      }
    };

    var f = this;

    // Load pages for items
    for (var index = 0; index < Object.values(t).length; index += 4) {
      loadItemPage(index);
    }

    // Update progress display
    var remainingItems = currentLevel - o;
    var progress = (remainingItems > 0) ? remainingItems - 1 : 0;
    this.progressNode.progress = progress / itemDetails.length;
    var collectedCount = Math.min(progress, itemDetails.length);
    this.num.string = "收集进度:" + collectedCount + "/" + itemDetails.length;

  };
  _ctor.prototype.tabSelect = function (t) {
    // Get the index of the selected toggle item in the toggleItems array
    var selectedIndex = this.tabNode.toggleItems.indexOf(t);
    cc.log(selectedIndex);

    var offset = 0;

    // Sum the values in the array 'h' up to the selected index
    for (var i = 0; i < selectedIndex; i++) {
      offset += h[i];
    }

    // Load the JSON data, passing the next index and the accumulated offset
    this.loadingJson(selectedIndex + 1, offset);
  };
  _ctor.prototype.onBtnLeft = function () {
    $zqcc_OverallSystem.mgrs().soundMgr.playBtnSound();
    this.list.setCurrentPageIndex(this.list.getCurrentPageIndex() - 1);
  };
  _ctor.prototype.onBtnRight = function () {
    $zqcc_OverallSystem.mgrs().soundMgr.playBtnSound();
    this.list.setCurrentPageIndex(this.list.getCurrentPageIndex() + 1);
  };
  _ctor.prototype.OnBtnClose = function () {
    $zqcc_OverallSystem.mgrs().soundMgr.playBtnSound();
    this.onlyClose();
  };
  cc__decorate([ccp_property(cc.PageView)], _ctor.prototype, "list", undefined);
  cc__decorate([ccp_property(cc.Node)], _ctor.prototype, "listItem", undefined);
  cc__decorate([ccp_property(cc.ProgressBar)], _ctor.prototype, "progressNode", undefined);
  cc__decorate([ccp_property(cc.Label)], _ctor.prototype, "num", undefined);
  cc__decorate([ccp_property(cc.ToggleContainer)], _ctor.prototype, "tabNode", undefined);
  return cc__decorate([ccp_ccclass], _ctor);
}(l.default);
exports.default = def_Tujian;